<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" labelWidth="85" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="part_report_revenueAndExpenditure_transverseIndex"
      :columns="columns"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :isSelectColumn="false"
      :onSyncTableData="tableHandle"
      :isMemoryPagination="false"
      :dataSource="dataSource"
      :isExportExcel="false"
      exportFileName="材料收支汇总.xlsx"
      :onSummationChange="handleTableSummation"
    >
      <!-- :fetchapi="fetchApi"
      :params="fetchParams" -->
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" icon="el-icon-printer" @click="handlePrint">打印</el-button>
        <AjaxButton size="small" type="primary" icon="el-icon-upload2" :click="handleExport">导出</AjaxButton>
      </template>
    </FilterTable>

    <!-- 打印组件 -->
    <BasePrint
      ref="print"
      :data="printData"
      :printerType="printParams.vPrintType == 99901002 ? 'stylus' : 'laser'"
      :alwaysPrint="printParams.vPageType == 99911002 ? true : false"
      :printCopies="printParams.nPages || 1"
      :direction="printParams.vPageDirection == 99921001 ? 'horizontal' : 'vertical'"
      :directPrint="printParams.vDirectPrint == 1 ? true : false"
      :isDuplex="printParams.vBothPrint == 1 ? true : false"
      template="part/report/templateRevenueAndExpenditureTransver"
    />
  </div>
</template>

<script>
/*
 *
 * 材料收支汇总--  横向报表.
 */

import { authority } from '@/utils/authMixin';
import { materialInfo, getExcelList } from '@/api/part/report/revenueAndExpenditure';
import moment from 'moment';
import { exportExcelFile, notifyAction } from '@/utils';
import { dictionary } from '@/utils/dictMixin';
import { getUser } from '@/assets/js/auth';
import { dealerPrintApi } from '@/api/repair/index.js';

export default {
  name: 'clszhzhxbbcs',
  components: {},
  mixins: [authority, dictionary],
  props: {
    // ptype: String
  },
  data() {
    return {
      visible: false,
      topFilterList: this.createTopFilters(),
      fetchApi: materialInfo,
      columns: this.createTableColumns(),
      fetchParams: { xhrAbort: true },
      formData: {}, // 向子组件传数据的数组
      tableData: [], // 表单数据
      queryParam: {}, // 查询条件
      dataSource: [], // 内存分页
      printData: {}, // 打印数据
      summationObj: {}, // 合计数据
      printParams: {} // 打印参数
    };
  },
  computed: {
    ptype() {
      console.log(this.$route.meta.ptype, 1283281838);

      return this.$route.meta.ptype;
    } // 模块区分
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.queryPrintParams();
  },
  methods: {
    // 搜索
    async changeHandle(val) {
      this.$refs.table.START_LOADING();
      this.queryParam = val;
      this.fetchParams = Object.assign({}, val, { ptype: this.ptype ? this.ptype : null });
      console.log(this.fetchParams, '查询参数');
      let res = await materialInfo(this.fetchParams);
      this.$refs.table.STOP_LOADING();
      this.dataSource = res.data;
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    // table动态回调函数
    tableHandle(val) {
      this.tableData = val;
      console.log(this.tableData);
    },
    async handleExport() {
      const option = {};
      console.log('查询参数', this.fetchParams);
      let res = await getExcelList(this.queryParam);
      exportExcelFile(res.data, '材料收支汇总');

      // getExcelList(this.queryParam).then(response => {
      //   console.log(response, '材料收支汇总');
      //   exportExcelFile(response.data, '材料收支汇总');
      //   // this.exportLoading = false;
      //   // this.exportExcel(res.records);
      // });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    createTopFilters() {
      return [
        {
          type: 'DATE',
          label: '查询日期',
          fieldName: 'date',
          initialValue: moment().format('YYYY-MM'),
          dateFormat: 'yyyy-MM',
          dateType: 'month'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '配件分类',
          dataIndex: 'PART_SORT_CODE',
          // sorter: true,
          // filter: true,
          // filterType: 'input',
          render: props => {
            return <span>{this.createDictText(props.row.PART_SORT_CODE, '8106')}</span>;
          }
        },
        {
          title: '期初',
          dataIndex: 'OPEN_AMOUNT',
          summation: true,
          precision: 2,
          width: 130
          // filter: true,
          // filterType: 'input'
        },
        {
          title: '收入',
          dataIndex: 'v5',
          align: 'center',
          children: [
            {
              title: '采购',
              dataIndex: 'BUY_IN_AMOUNT',
              sorter: true,
              filter: true,
              width: 130,
              filterType: 'number',
              summation: true,
              precision: 2
            },
            {
              title: '借进',
              dataIndex: 'ALLOCATE_IN_AMOUNT',
              sorter: true,
              filter: true,
              filterType: 'number',
              summation: true,
              precision: 2
            },
            {
              title: '盘点',
              dataIndex: 'INVENTORY_AMOUNT',
              sorter: true,
              filter: true,
              filterType: 'number',
              summation: true,
              precision: 2
            },
            {
              title: '收入合计',
              dataIndex: 'TOTAL_INCOME',
              sorter: true,
              filter: true,
              filterType: 'number',
              summation: true,
              precision: 2
            }
          ]
          // sorter: true,
          // filter: true,
          // filterType: 'input'
        },

        {
          title: '支出',
          dataIndex: 'vvv5',
          align: 'center',
          children: [
            {
              title: '维修与销售',
              dataIndex: 'ckMonthNum',
              // sorter: true,
              // filter: true,
              // filterType: 'number',
              align: 'center',
              children: [
                {
                  title: '首保',
                  dataIndex: 'FIRST_GUARANTEE',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                },
                {
                  title: '索赔',
                  dataIndex: 'CLAIMANT',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                },
                {
                  title: '保养',
                  dataIndex: 'MAINTENANCE',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                },
                {
                  title: '小修',
                  dataIndex: 'MINOR_REPAIR',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                },
                {
                  title: '大修',
                  dataIndex: 'OVERHAUL',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                },
                {
                  title: '事故',
                  dataIndex: 'ACCIDENT',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                },
                {
                  title: '返工',
                  dataIndex: 'REWORK',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                },
                {
                  title: '年审',
                  dataIndex: 'ANNUAL_REVIEW',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                },
                {
                  title: '其它',
                  dataIndex: 'OTHER_OUT_SALE_AMOUNT',
                  summation: true,
                  precision: 2,
                  width: 120
                  // sorter: true,
                  // filter: true,
                  // filterType: 'number'
                },
                {
                  title: '销售',
                  dataIndex: 'SALE_OUT_COST_AMOUNT',
                  // dataIndex: 'SALE_OUT_SALE_AMOUNT',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                },
                {
                  title: '维修销售小计',
                  dataIndex: 'SUBTOTAL_MAINTENANCE_SALES',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                }
              ]
            },
            {
              title: '其它',
              dataIndex: 'ckMonthAmount',
              align: 'center',
              // sorter: true,
              // filter: true,
              // filterType: 'number',
              children: [
                {
                  title: '内修',
                  dataIndex: 'INSURANCE',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                },
                {
                  title: '内部领用',
                  dataIndex: 'INNER_OUT_COST_AMOUNT',
                  // dataIndex: 'INNER_OUT_SALE_AMOUNT',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                },
                {
                  title: '还出',
                  dataIndex: 'ALLOCATE_OUT_COST_AMOUNT',
                  // dataIndex: 'ALLOCATE_OUT_SALE_AMOUNT',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                },
                {
                  title: '兑奖出库',
                  dataIndex: 'AWARD_OUT_COST_AMOUNT',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                },
                {
                  title: '其它小计',
                  dataIndex: 'OTHER_SUBTOTALS',
                  sorter: true,
                  filter: true,
                  filterType: 'number',
                  summation: true,
                  precision: 2
                }
              ]
            },
            {
              title: '支出合计',
              dataIndex: 'TOTAL_EXPENDITURE',
              summation: true,
              precision: 2
              // sorter: true,
              // filter: true,
              // filterType: 'number'
            }
          ],
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '结存',
          dataIndex: 'CLOSE_AMOUNT',
          align: 'center',
          width: 130,
          sorter: true,
          filter: true,
          filterType: 'input',
          summation: true,
          precision: 2
        }
      ];
    },
    // 点击打印
    handlePrint() {
      if (this.tableData.length) {
        this.tableData.map(item => {
          item.PART_SORT_CODE_NAME = this.createDictText(item.PART_SORT_CODE, '8106');
        });
        this.printData.dataList = this.tableData;
        this.printData.createBy = getUser();
        this.printData.date = this.topFilterList.find(item => item.fieldName == 'date').initialValue;
        this.printData.summationObj = this.summationObj;
        this.$refs.print.EXCUTE_PRINT();
      } else {
        notifyAction('请先查询数据', 'warning');
      }
    },
    handleTableSummation(e) {
      let obj = {};
      e.map(item => {
        obj[item.dataIndex] = item.value;
      });
      this.summationObj = obj;
    },
    // 查询打印参数
    async queryPrintParams() {
      let res = await dealerPrintApi({
        vModule: '90211009',
        vNormalCode: 'BJCLSZ'
      });
      if (res.resultCode == 200 && res.data.length) {
        this.printParams = res.data[0];
      }
      console.log(this.printParams, '打印参数');
    }
  }
};
</script>

<style lang="less" scoped></style>
